package com.xray.netdisk.service.impl;

import com.xray.netdisk.pojo.FileItem;
import com.xray.netdisk.service.FileService;
import com.xray.netdisk.utils.JDBCUtils;

import java.util.List;

/**
 * 文件逻辑实现
 */
public class FileServiceImpl implements FileService {

    @Override
    public List<FileItem> getFileItemsByUserId(Long userId) {
        return JDBCUtils.queryList(FileItem.class,"select * from t_file where user_id=?",userId);
    }

    @Override
    public FileItem getFileItemByUserIdAndFilename(Long userId, String filename) {
        List<FileItem> fileItems = JDBCUtils.queryList(FileItem.class, "select * from t_file where user_id = ? and filename=?", userId,filename);
        if(!fileItems.isEmpty()){
            return  fileItems.get(0);
        }
        return null;
    }

    @Override
    public FileItem getFileItemById(Long id) {
        List<FileItem> fileItems = JDBCUtils.queryList(FileItem.class, "select * from t_file where id = ?", id);
        if(!fileItems.isEmpty()){
            return  fileItems.get(0);
        }
        return null;
    }

    @Override
    public void modifyFileItem(FileItem fileItem) {
        JDBCUtils.update("update t_file set filename=?,path=?,length=?,user_id=?,open=?,create_time=? where id=?",
                fileItem.getFilename(),fileItem.getPath(),fileItem.getLength(),fileItem.getUser_id(),fileItem.getOpen(),fileItem.getCreateTime(),fileItem.getId());
    }

    @Override
    public void addFileItem(FileItem fileItem) {
        JDBCUtils.update("insert into t_file(filename,path,length,user_id,open,create_time) values(?,?,?,?,?,?)",
                fileItem.getFilename(),fileItem.getPath(),fileItem.getLength(),fileItem.getUser_id(),fileItem.getOpen(),fileItem.getCreateTime());
    }

    @Override
    public void removeFileItem(Long id) {
        JDBCUtils.update("delete from t_file where id=?",id);
    }
}
